<template>
  <el-dialog :title="titleStr" :visible.sync="data.bSwitch" width="500px">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="110px"
      class="demo-ruleForm"
    >
      <el-form-item label="优惠券名称" prop="name">
        <el-input
          v-model="ruleForm.name"
          placeholder="请输入"
          style="width: 270px"
        ></el-input>
      </el-form-item>

      <el-form-item label="优惠券类型" prop="type">
        <el-radio-group v-model="ruleForm.type">
          <el-radio :label="1">金额券</el-radio>
          <el-radio :label="2">折扣券</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="折扣" prop="discount" v-if="ruleForm.type === 2">
        <el-input-number
          v-model="ruleForm.discount"
          controls-position="right"
          :min="0"
          :step="0.05"
          step-strictly
          style="width: 200px"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="金额（元）" prop="amount" v-if="ruleForm.type === 1">
        <el-input-number
          v-model="ruleForm.amount"
          controls-position="right"
          :min="0"
          :precision="2"
          placeholder="请输入"
          style="width: 200px"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="发放数量" prop="totalNum">
        <el-input-number
          v-model="ruleForm.totalNum"
          controls-position="right"
          :min="0"
          :step="1"
          step-strictly
          placeholder="请输入"
          style="width: 200px"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="是否限领" prop="personLimited">
        <el-radio-group v-model="ruleForm.personLimited">
          <el-radio :label="0">无限制</el-radio>
          <el-radio :label="1">有限制</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        label="限领次数"
        prop="personNum"
        v-if="ruleForm.personLimited === 1"
      >
        <el-input-number
          v-model="ruleForm.personNum"
          controls-position="right"
          :min="1"
          :step="1"
          step-strictly
          placeholder="请输入"
          style="width: 200px"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="状态" prop="enabled">
        <el-switch
          v-model="ruleForm.enabled"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          type="textarea"
          v-model="ruleForm.remark"
          placeholder="请输入"
          style="width: 300px"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="data.bSwitch = false">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addCoupon, getCoupon, updateCoupon } from "@/api/coupon";

export default {
  props: {
    data: {
      type: Object,
      default() {
        return {
          id: null,
          bSwitch: false,
        };
      },
    },
  },
  data() {
    return {
      ruleForm: {
        name: null,
        type: 1,
        totalNum: undefined,
        discount: 1,
        amount: undefined,
        enabled: 0,
        remark: null,
        personNum: 1,
        personLimited: 1,
      },
      rules: {
        name: [{ required: true, message: "请输入", trigger: "blur" }],
        type: [{ required: true, message: "请选择", trigger: "change" }],
        totalNum: [{ required: true, message: "请输入", trigger: "blur" }],
        discount: [{ required: true, message: "请输入", trigger: "blur" }],
        amount: [{ required: true, message: "请输入", trigger: "blur" }],
        personNum: [{ required: true, message: "请输入", trigger: "blur" }],
        personLimited: [
          { required: true, message: "请选择", trigger: "change" },
        ],
        enabled: [{ required: true, message: "请选择", trigger: "change" }],
      },
    };
  },
  computed: {
    titleStr() {
      return this.data.id ? "修改" : "新增";
    },
  },
  created() {
    this.data.id && this.getDetail();
  },
  methods: {
    // 获取详情
    getDetail() {
      getCoupon(this.data.id).then((res) => {
        const {
            name,
            type,
            totalNum,
            subMoney,
            enabled,
            remark,
            personNum,
            personLimited,
        } = res.data
        this.$set(this.ruleForm, 'name', name)
        this.$set(this.ruleForm, 'type', type)
        this.$set(this.ruleForm, 'totalNum', totalNum)
        this.$set(this.ruleForm, 'enabled', enabled)
        this.$set(this.ruleForm, 'remark', remark)
        this.$set(this.ruleForm, 'personNum', personNum)
        this.$set(this.ruleForm, 'personLimited', personLimited)
        if (type === 1) {
            this.$set(this.ruleForm, 'amount', subMoney)
        } else if (type === 2) {
            this.$set(this.ruleForm, 'discount', subMoney)
        }
      });
    },
    // 提交
    onSubmit() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          const {
            name,
            type,
            totalNum,
            discount,
            amount,
            enabled,
            remark,
            personNum,
            personLimited,
          } = this.ruleForm;
          const params = {
            name,
            type,
            totalNum,
            enabled,
            remark,
            personNum,
            personLimited,
          };
          if (type === 1) {
            params.subMoney = amount;
          } else {
            params.subMoney = discount;
          }
          if (this.data.id) {
            this.updateSubmit({ uuid: this.data.id, ...params });
          } else {
            this.addSubmit(params);
          }
        } else {
          return false;
        }
      });
    },
    // 新增
    addSubmit(params) {
      addCoupon(params).then((res) => {
        this.$emit("refresh");
        this.$modal.msgSuccess("成功");
        this.data.bSwitch = false;
      });
    },
    // 修改
    updateSubmit(params) {
      updateCoupon(params).then((res) => {
        this.$emit("refresh");
        this.$modal.msgSuccess("成功");
        this.data.bSwitch = false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left; /* 将文本对齐方式改为左对齐，这样数字就不会居中了 */
}
</style>